﻿using System;
using UnityEngine;

public class Follow : MonoBehaviour
{
    public Vector3 offset;
    public Options options = new Options();
    public float speed = 5f;
    public Transform target;
    private Vector3 targetPos;

    public void SetTargetImmediately(Transform value)
    {
        this.target = value;
        this.Update(10000f);
    }

    protected virtual void Update()
    {
        this.Update(this.speed);
    }

    private void Update(float speed)
    {
        if (this.target != null)
        {
            this.targetPos = this.target.position;
        }
        Vector3 position = base.transform.position;
        if (this.options.positionX)
        {
            position.x = this.targetPos.x + this.offset.x;
        }
        if (this.options.positionY)
        {
            position.y = this.targetPos.y + this.offset.y;
        }
        if (this.options.positionZ)
        {
            position.z = this.targetPos.z + this.offset.z;
        }
        base.transform.position = Vector3.Lerp(base.transform.position, position, Time.deltaTime * speed);
    }

    [Serializable]
    public class Options
    {
        public bool positionX = true;
        public bool positionY = true;
        public bool positionZ = true;
    }
}

